package top.hidoctor.auscultation.net.p;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.internal.LinkedTreeMap;
import com.google.gson.reflect.TypeToken;
import com.tang.base.BaseResponse;
import com.tang.base.MapParamsRequest;
import com.tang.base.NetMapEncryptUtils;
import com.tang.base.PatchRequest;
import com.tang.net.SuccessConsumer;
import com.tang.user.UserInfoManager;
import com.tang.util.GsonUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.functions.Consumer;
import top.hidoctor.auscultation.bean.MarkDetailBean;
import top.hidoctor.auscultation.bean.ProblemBean;
import top.hidoctor.auscultation.bean.SPBean;
import top.hidoctor.auscultation.net.PresenterWrapper;
import top.hidoctor.auscultation.net.c.WorkbenchContract;
import top.hidoctor.auscultation.net.c.WorkbenchShContract;
import top.hidoctor.auscultation.net.req.AuditUserReq;
import top.hidoctor.auscultation.net.req.DictionaryReq;
import top.hidoctor.auscultation.net.req.MarkReq;

public class WorkbenchShPresenter extends PresenterWrapper<WorkbenchShContract.View> implements WorkbenchShContract.Presenter {


    public WorkbenchShPresenter(Context context, WorkbenchShContract.View view) {
        super(context, view);
    }


    /**
     * 获取审核标注详情
     *
     * @param id
     * @param SampleId
     */
    @Override
    public void getMarkDetail(String id, String SampleId) {
        mView.showLoading();
        Map<String, Object> maps = new NetMapEncryptUtils("").getMaps();
        maps.put("operator", UserInfoManager.getInstance().getUserInfo().getUserId());
        add(mService.getAuditDetail(id, SampleId, maps)
                .compose(this.<MarkDetailBean>getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<MarkDetailBean>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<MarkDetailBean> response) {
                        mView.hideLoading();
                        mView.getMarkDetailSuc(response.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }

    /**
     * 获取仲裁标注详情
     *
     * @param id
     * @param SampleId
     * @param markId
     */
    @Override
    public void getArbitrationDetail(String id, String SampleId, String markId) {
        mView.showLoading();
        Map<String, Object> maps = new HashMap<>();
        add(mService.getArbitrationDetail(id, SampleId, markId)
                .compose(this.<MarkDetailBean>getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<MarkDetailBean>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<MarkDetailBean> response) {
                        mView.hideLoading();
                        mView.getMarkDetailSuc(response.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }

    /**
     * 获取医生详情列表
     *
     * @param labelId
     * @param SampleId
     */
    @Override
    public void getMarksDetail(String labelId, String SampleId) {
        mView.showLoading();
        add(mService.getMarkDetasils(labelId, SampleId)
                .compose(this.<List<MarkDetailBean>>getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<List<MarkDetailBean>>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<List<MarkDetailBean>> response) {
                        mView.hideLoading();
                        mView.getMarksDetailSuc(response.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }

    @Override
    public void downloadFile(String fileId) {
        mView.showLoading();
        Map<String, Object> map = new HashMap<>();
        map.put("path", fileId);
        add(mService.downloadFile("2", map)
                .compose(this.getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<Object>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<Object> response) {
                        mView.hideLoading();
                        String data = (String) response.getData();
                        SPBean spBean = GsonUtil.parseJson(data, SPBean.class);
                        mView.downloadFileSuc(spBean);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }

    @Override
    public void getDictionaryList1(final DictionaryReq req) {
        mView.showLoading();
        Map<String, Object> maps = new NetMapEncryptUtils(req).getMaps();
        add(mService.dictionaryList(maps)
                .compose(this.getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<Object>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<Object> response) {
                        mView.hideLoading();
                        String data = GsonUtil.toJsonStr(((LinkedTreeMap) response.getData()).get("result"));
                        if (!TextUtils.isEmpty(data)) {
                            List<ProblemBean> problemBeans = GsonUtil.parseJson(data, new TypeToken<List<ProblemBean>>() {
                            });
                            mView.getDictionaryListSuc1(problemBeans);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }

    @Override
    public void getDictionaryList2(final DictionaryReq req) {
        mView.showLoading();
        Map<String, Object> maps = new NetMapEncryptUtils(req).getMaps();
        add(mService.dictionaryList(maps)
                .compose(this.getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<Object>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<Object> response) {
                        mView.hideLoading();
                        String data = GsonUtil.toJsonStr(((LinkedTreeMap) response.getData()).get("result"));
                        if (!TextUtils.isEmpty(data)) {
                            List<ProblemBean> problemBeans = GsonUtil.parseJson(data, new TypeToken<List<ProblemBean>>() {
                            });
                            mView.getDictionaryListSuc2(problemBeans);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }

    /**
     * 更新 医生评分标注详情  单个
     *
     * @param labelId
     * @param sampleId
     * @param markId
     * @param req
     */
    @Override
    public void updataMark(final String labelId, final String sampleId, String markId, MarkReq req) {
        mView.showLoading();
        Map<String, Object> maps = new NetMapEncryptUtils(req).getMaps();
        PatchRequest request = new PatchRequest(maps);
        add(mService.updataMark(labelId, sampleId, markId, request.getRequestBody())
                .compose(this.getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<Object>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<Object> response) {
                        mView.hideLoading();
                        getMarksDetail(labelId, sampleId);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }

    /**
     * 更新审核标注详情
     *
     * @param id
     * @param labelId
     * @param sampleId
     * @param markId
     * @param req
     */
    @Override
    public void updateAuditDetail(final String id, final String labelId, final String sampleId, String markId, MarkReq req) {
        mView.showLoading();
        Map<String, Object> maps = new NetMapEncryptUtils(req).getMaps();
        PatchRequest request = new PatchRequest(maps);
        add(mService.updateAuditDetail(labelId, sampleId, markId, request.getRequestBody())
                .compose(this.getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<Object>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<Object> response) {
                        mView.hideLoading();
                        getMarkDetail(id, sampleId);
                        mView.updateAuditDetailSuc();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }

    /**
     * 更新仲裁详情
     *
     * @param id
     * @param labelId
     * @param sampleId
     * @param markId
     * @param req
     */
    @Override
    public void updataArbitrationDetail(final String id, final String labelId, final String sampleId, String markId, MarkReq req) {
        mView.showLoading();
        Map<String, Object> maps = new NetMapEncryptUtils(req).getMaps();
        PatchRequest request = new PatchRequest(maps);
        add(mService.updataArbitration(id, sampleId, markId, request.getRequestBody())
                .compose(this.getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<Object>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<Object> response) {
                        mView.hideLoading();
                        getArbitrationDetail(id, sampleId, id);
                        mView.updateAuditDetailSuc();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }

    /**
     * 更新审核 状态
     *
     * @param id
     * @param userId
     * @param sampleId
     * @param req
     */
    @Override
    public void updataAudit(String id, String userId, String sampleId, AuditUserReq req) {
        mView.showLoading();
        Map<String, Object> maps = new NetMapEncryptUtils(req).getMaps();
        PatchRequest request = new PatchRequest(maps);
        add(mService.updataAudit(id, userId, sampleId, request.getRequestBody())
                .compose(this.getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<Object>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<Object> response) {
                        mView.hideLoading();
                        mView.updataAuditSuc();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }

    @Override
    public void deleteArbitration(String id) {
        mView.showLoading();
        Map<String, Object> maps = new NetMapEncryptUtils("").getMaps();
        add(mService.deleteArbitration(id, maps)
                .compose(this.getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<Object>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<Object> response) {
                        mView.hideLoading();
                        mView.updataAuditSuc();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }


}
